/**
 * Created by zhangqiqi1 on 2015/12/28.
 */

function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
    //返回参数值
    if (r != null) return unescape(r[2]);
    return null;
};

function callbackFun(){
    var argsArray = Array.prototype.slice.call(arguments);
    var backFn = argsArray.pop();
    return function(result){
        argsArray.push(result);
//            backFn(argsArray);
        backFn.apply(this,argsArray);
    };
};

/**
 * Date转为字符串
 * @param nS
 * @returns {string}
 */
function getLocalTime(nS) {
    if(typeof nS == "undefined" || nS == null){
        return "";
    }
    return new Date(parseInt(nS)).toLocaleString().replace(/年|月|\//g, "-").replace(/日/g, " ").replace(/上午|下午/,' ');

}

function getNotNullString(str){
    if(typeof str == "undefined" || str == null){
        return "";
    }
    return str;
}

function serializeObject(list) {
    var o = {};
    $.each(list, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
}

function serializeJson(list) {
    var o = {};
    $.each(list, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return JSON.stringify(o);
}

function serializeJsonIgnoreEmpty(list) {
    var o = {};
    $.each(list, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            if(this.value!=''){
                o[this.name].push(this.value || '');
            }
        } else {
            if(this.value!=''){
                o[this.name] = this.value || '';
            }
        }
    });
    return JSON.stringify(o);
}

/**
 * 根据状态码获取状态
 * @param state
 * @returns {string}
 */
function getStateStr(state){
    var stateStr = "";
    if(state == 03){
        stateStr = "未定义";
    }else if(state == 10) {
        stateStr = "定义中";
    }else if(state ==20){
        stateStr = "开发中";
    }else if(state ==25){
        stateStr = "在线修改";
    }else if(state ==30){
        stateStr = "上线";
    }else if(state ==40){
        stateStr = "暂停";
    }else if(state ==50){
        stateStr = "下线";
    }else {
        stateStr = "错误的状态";
    }
    return stateStr;
}

/**
 * 根据大类获取产品列表
 * @param busiId
 * @param productSelect
 * @param notAll 如果为true，则不添加全部的选项
 */
function queryProduct(busiId, productSelect, notAll){
    var action = "/home/getProduct";
    ajax_request(action,{
        busiId : busiId
    },function(result) {
        var html = "<option value=\"\">全部</option>";
        if(notAll){
            html = "";
        }
        for(var index = 0; index < result.productList.length; index ++){
            var product = result.productList[index];
            html += "<option value=\"" + product.firstUpper + "\">" + product.productname + "</option>";
        }
        $(productSelect).html(html);
        $(productSelect).val("");
    });
}

/**
 * 根据产品ID获取维度总线列表
 * @param productid
 * @param dimensionBusSelect
 * @param notAll 如果为true，则不添加全部的选项
 */
function queryDimensionBus(productid, dimensionBusSelect, notAll){
    var action = "/home/getDimensionBus";
    ajax_request(action,{
        productid : productid
    },function(result) {
        var html = "<option value=\"\">全部</option>";
        if(notAll){
            html = "";
        }
        for(var index = 0; index < result.dimensionBuses.length; index ++){
            var dimensionBus = result.dimensionBuses[index];
            html += "<option value=\"" + dimensionBus.dimensionbusid + "\">" + dimensionBus.dimensionbusname + "</option>";
        }
        $(dimensionBusSelect).html(html);
        $(dimensionBusSelect).val("");
    });
}

function queryDimensionBusByBusi(busiid, dimensionBusSelect, notAll){
    $("#dimensionTable").find("tbody").find("tr").each(function(){
        $(this).remove();
    });
    var action = "/dimension/queryBusiDimensions";
    ajax_request(action, {
        busiid: busiid
    }, function (result) {
        var html = "<option value=\"\">全部</option>";
        if(notAll){
            html = "";
        }
        for(var index = 0; index < result.listBusinessDimensions.length; index ++){
            var dimensionBus = result.listBusinessDimensions[index];
            html += "<option value=\"" + dimensionBus.dimensionbusid + "\">" + dimensionBus.dimensionBusName + "</option>";
        }
        $(dimensionBusSelect).html(html);

    });
}

function queryProductBusies(productid, dimensionBusSelect, notAll){
    $("#dimensionTable").find("tbody").find("tr").each(function(){
        $(this).remove();
    });
    var action = "/dimension/queryProductDimensions";
    ajax_request(action, {
        productid: productid
    }, function (result) {
        var html = "<option value=\"\">全部</option>";
        if(notAll){
            html = "";
        }
        for(var index = 0; index < result.listProductDimensions.length; index ++){
            var dimensionBus = result.listProductDimensions[index];
            html += "<option value=\"" + dimensionBus.dimensionbusid + "\">" + dimensionBus.dimensionbusname + "</option>";
        }
        $(dimensionBusSelect).html(html);

    });
}

function formatDate(date, fmt) { //author: meizz
    var o = {
        "M+" : date.getMonth()+1,                 //月份
        "d+" : date.getDate(),                    //日
        "h+" : date.getHours(),                   //小时
        "m+" : date.getMinutes(),                 //分
        "s+" : date.getSeconds(),                 //秒
        "q+" : Math.floor((date.getMonth()+3)/3), //季度
        "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}

function trim(str){ //删除左右两端的空格
  return str.replace(/(^\s*)|(\s*$)/g, "");
 }